package com.technology.education.ui.market.popularize

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.*
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.AppData
import com.technology.education.data.model.Record
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.*
import me.hgj.jetpackmvvm.ext.parseState

class PopularizeFragment : BaseRecyclerTopBarFragment<PopularizeViewModel>() {
    override fun initViews() {
        setTitle("推广中心")
        noToolbarBack()
        enableSwipe()
        setToolBarRight(R.menu.spreader_menu){
            navigation(requireView(),R.id.action_main_fragment_to_settingFragment)
        }
    }

    override fun refreshData() {
        super.refreshData()
        getAppViewModel().getInfo(true)
    }

    override fun lazyLoadData() {
        loading()
        setUI()
    }


    private fun setUI() {
        renderAdapter {
            clearAllItems()
            updateOrInsertHeaderItem<DslAdapterItem>("code", 0) {
                it.apply {
                    itemLayoutId = R.layout.item_invitated_code
                    itemPaddingTop = 24 * dpi
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.tv(R.id.tv_code)?.text = getAppViewModel().userInfo.value?.inviteCode?:"未获取到"
                        itemHolder.click(R.id.iv_copy) {
                            (getAppViewModel().userInfo.value?.inviteCode ?: "").copyToClip(
                                requireContext()
                            )
                            toast("邀请码已复制到剪切板")
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<DslAdapterItem>("mine", 1) {
                it.apply {
                    itemLayoutId = R.layout.item_mine_market
                    itemTag = "time"
                    itemPaddingTop = 11 * dpi
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.tv(R.id.tv_time_remain)?.text =
                            getAppViewModel().userInfo.value?.balanceMoney.toString()
                        itemHolder.invisible(R.id.tv_time_record)
                        itemHolder.tv(R.id.textview)?.text = "我的余额"
                        itemHolder.click(R.id.tv_pro){
                            toWebActivity("积分规则",AppData.SCORE_PROTOCOL)
                        }
                        itemHolder.click(R.id.tv_pay) {view->
                            navigation(
                                view,
                                R.id.action_main_fragment_to_withdrawFragment,
                                bundleOf("type" to 3)
                            )
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<DslAdapterItem>("menu", 2) {
                it.apply {
                    itemLayoutId = R.layout.item_popularize_menu
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemTopInsert = 16 * dpi
                        itemHolder.click(R.id.tv_mine) { it ->
                            ///我的推广
                            navigation(it, R.id.action_main_fragment_to_popularizeRecordFragment)
                        }
                        itemHolder.click(R.id.tv_invite) {
                            //todo:立即推广
                        }
                        itemHolder.click(R.id.tv_record) { it ->
                            //积分记录
                            navigation(
                                it,
                                R.id.action_main_fragment_to_withdrawRecordFragment
                            )
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<ItemSingleText>("title", 4) {
                it.apply {
                    title = "余额记录"
                    style.isBold = true
                    style.bottomPadding = 0
                }
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(
                        it.list.toDslItemList(R.layout.item_score_record) {
                            itemBindOverride = { itemHolder, _, _, _ ->
                                itemData?.let { data ->
                                    if (data is Record) {
                                        itemHolder.tv(R.id.tv_title)?.text = data.title
                                        itemHolder.tv(R.id.tv_score)?.text = data.valueString
                                        itemHolder.tv(R.id.tv_date)?.text = data.createDate

                                    }
                                }
                            }
                        },
                        mViewModel.pageIndex
                    )
                }
            }, onError = {
                loadError()
            })
        }

        getAppViewModel().userInfo.observe(this) {
            renderAdapter {
                findItemByTag("mine")?.updateAdapterItem()
                findItemByTag("code")?.updateAdapterItem()
            }
        }
    }
}